﻿using AntdUI;
using BST250301TestStand.Models;
using MySql.Data.MySqlClient;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BST250301.Views
{
    public partial class ShowDialog : Form
    {
        public bool submit;
        public bool _isAutoConfirm;
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private int _seconds = 0;
        private string _msg = string.Empty;
        /// <summary>
        ///自动关闭时为传入isAutoConfirm值
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="seconds"></param>
        /// <param name="isAutoConfirm"></param>
        public ShowDialog(string msg, int seconds = 0,bool isAutoConfirm =true)
        {
            InitializeComponent();
            this._seconds = seconds;
            this._msg = msg;
            this._isAutoConfirm = isAutoConfirm;
            this.label1.Text = msg;
            if (seconds > 0)
            {
                this.buttonAdd.Enabled = false;
                this.button1.Enabled = true;
                timer.Interval = 1000;
                timer.Enabled = true;
                timer.Tick += Timer_Tick;
                timer.Start();
            }
            this.TopMost = true;
            this.FormBorderStyle= FormBorderStyle.FixedSingle;
        }

        private void Timer_Tick(object? sender, EventArgs e)
        {
            this.Invoke(() =>
            {
                this.label1.Text = $"{this._msg},请等待读秒完成:{_seconds} ...";
            });
            _seconds--;
            if (_seconds < 0)
            {
                this.timer.Stop();
                this.buttonAdd.Enabled = true;
                submit =this._isAutoConfirm;
                this.Dispose();
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            submit = true;
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer.Stop();
            timer.Enabled=false;
            submit = false;
            this.Dispose();
        }
    }
}
